package commucation;
import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.broker.BrokerService;

public class MessagesReceiver {
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;
    private TerrainMessageListener listener = new TerrainMessageListener();
    
    public TerrainMessageListener getListener(){
    	return listener;
    }

    public void listenOn(String topicName) throws JMSException {

        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        Session session = connection.createSession(false,
                Session.AUTO_ACKNOWLEDGE);

        Topic topic = session.createTopic(topicName);

        MessageConsumer consumer = session.createConsumer(topic);

        
        
        consumer.setMessageListener(listener);
        System.out.println("Attente des messages ...");
        /*try {
            Thread.sleep(20000);
        }
        catch (java.lang.InterruptedException ie) {
            ie.printStackTrace(System.err);
        }
        System.out.println("Fermeture de la connexion.");
        connection.close();*/

    }
}